package org.cloud.im.admin.handler;

import org.cloud.im.admin.account.bo.UserInfoBO;
import org.cloud.im.core.packets.ChatBody;
import org.cloud.im.core.packets.Group;
import org.cloud.im.core.packets.User;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

/**
 * @Author: w.weidong.lin@abite.com
 * @Description: 消息存储
 * @Date: create in 2021/10/26 21:01
 */
@Component
public class StoreHandler {
    /**
     * 聊天记录
     */
    public static final Map<String, List<ChatBody>> chatRecordMap = new ConcurrentHashMap<>();
    /**
     * 用户新
     */
    public static Map<String, User> userInfoMap = new ConcurrentHashMap<>();
    /**
     * 部门信息
     */
    public static Map<String, List<UserInfoBO>> deptInfoMap = new ConcurrentHashMap<>();
    /**
     * 用户群组信息
     */
    public static Map<String, List<UserInfoBO>> userGroupMap = new ConcurrentHashMap<>();

    /**
     * 为了测试方便，启动后初始化一些数据
     */
    public static void initStoreData() {
        List<UserInfoBO> users = new ArrayList<>();
        users.add(new UserInfoBO("Z000001", "行政", "admin", "超管"));
        users.add(new UserInfoBO("Z000001", "行政", "dong", "东咚咚"));
        users.add(new UserInfoBO("Z000001", "行政", "20010610", "戴老板"));
        users.add(new UserInfoBO("Z000001", "行政", "60013358", "董巍"));
        users.add(new UserInfoBO("Z000001", "行政", "20010608", "黄剑"));
        users.add(new UserInfoBO("S011837", "税务部", "admin", "超管"));
        users.add(new UserInfoBO("S011837", "税务部", "60013357", "王灿"));
        users.add(new UserInfoBO("S011837", "税务部", "60013355", "东升"));


        StoreHandler.deptInfoMap = users.stream().collect(Collectors.groupingBy(o -> o.getDeptNo()));
        StoreHandler.userGroupMap = users.stream().collect(Collectors.groupingBy(o -> o.getEmpNo()));

        users.forEach( u -> {
            User user = User.newBuilder()
                    .userId(u.getEmpNo())
                    .nick(u.getEmpName())
                    .build();
            user.setFriends(getUserGroup(u.getEmpNo()));
            user.setGroups(getGroupInfo(u.getEmpNo()));
            userInfoMap.put(u.getEmpNo(), user);
        });
    }


    /**
     * 存储聊天信息
     *
     * @param chatBody
     */
    public static void storeChat(ChatBody chatBody) {
        putUserChat(chatBody.getFrom() + chatBody.getTo(), chatBody);
        putUserChat(chatBody.getTo() + chatBody.getFrom(), chatBody);
    }

    private static void putUserChat(String key, ChatBody chatBody) {
        List<ChatBody> chats = chatRecordMap.get(key);
        if (chats == null) {
            chats = new ArrayList<>();
        }
        chats.add(chatBody);
        chatRecordMap.put(key, chats);
    }

    /**
     * 用户聊天数据
     *
     * @param from
     * @param to
     * @return
     */
    public static List<ChatBody> getUserChatInfo(String from, String to) {
        return chatRecordMap.get(from + to);
    }

    /**
     * 设置用户信息
     *
     * @param map
     */
    public static void setUseInfoMap(Map<String, UserInfoBO> map) {
        if (map != null) {
            map.forEach((key, value) -> {
                User user = User.newBuilder()
                        .userId(value.getEmpNo())
                        .nick(value.getEmpName())
                        .build();
                user.setFriends(getUserGroup(key));
                user.setGroups(getGroupInfo(key));
                userInfoMap.put(key, user);
            });
        }
    }

    /**
     * 用户个人信息
     *
     * @param userId
     * @return
     */
    public static User getUserInfo(String userId) {
        return userInfoMap.get(userId);
    }

    /**
     * 获取群组信息
     *
     * @param userId
     * @return
     */
    public static List<Group> getGroupInfo(String userId) {
        List<Group> myGroups = new ArrayList<>();
        if (userId == null) {
            return myGroups;
        }
        List<UserInfoBO> groupList = StoreHandler.userGroupMap.get(userId);
        if (groupList == null || groupList.size() < 1) {
            return myGroups;
        }

        groupList.forEach(dept -> {
            Group mg = Group.newBuilder().groupId(dept.getDeptNo()).name(dept.getDeptName()).build();
            myGroups.add(mg);
        });
        return myGroups;
    }

    /**
     * 用户群组及组员新
     *
     * @param userId
     * @return
     */
    public static List<Group> getUserGroup(String userId) {
        List<Group> userGroupList = new ArrayList<>();
        if (userId == null) {
            return userGroupList;
        }
        List<UserInfoBO> groupList = StoreHandler.userGroupMap.get(userId);
        if (groupList == null || groupList.size() < 1) {
            return userGroupList;
        }

        groupList.forEach(dept -> {
            Group myFriend = Group.newBuilder().groupId(dept.getDeptNo()).name(dept.getDeptName()).build();

            List<User> userVOS = new ArrayList<>();
            List<UserInfoBO> users = StoreHandler.deptInfoMap.get(dept.getDeptNo());
            if (users != null && users.size() > 0) {
                users.forEach(o -> {
                    User user = User.newBuilder()
                            .userId(o.getEmpNo())
                            .nick(o.getEmpName())
                            .build();
                    userVOS.add(user);
                });
            }
            myFriend.setUsers(userVOS);
            userGroupList.add(myFriend);
        });
        return userGroupList;
    }
}
